package com.example.analysis.controller;

import com.example.analysis.entity.Compete;
import com.example.analysis.entity.Competition;
import com.example.analysis.entity.Election;
import com.example.analysis.entity.MessageInfo;
import com.example.analysis.service.*;
import com.example.analysis.utils.ComExcelUtils;
import com.example.analysis.utils.ExcelUtils;
import com.example.analysis.utils.Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class ExcelUpdate {
    @Resource
    private ClassService classService;
    @Resource
    private CollegeService collegeService;
    @Resource
    private MajorService majorService;
    @Resource
    private CourseService courseService;
    @Resource
    private StudentService studentService;
    @Resource
    private CompeteService competeService;
    @Resource
    private ElectionService electionService;



    public String getMajorId01(String major){
        String temp=major.replaceAll("\\d.*", "");
        if(temp.equals("物联网")){
            temp="物联";
        }
        return temp;
    }

    @PostMapping("/fileUpdate")
    public String fileUpdate(MultipartFile file) {
        long startTime = System.currentTimeMillis();
        List<String> list = new ArrayList();
        Map<String,Object> res = new HashMap<>();
        int row=1;
        int rowSuccess=0;
        Integer errorCount=0;
        //导入Class表
        int count=1;
        List<MessageInfo> messageList = ExcelUtils.getExcelInfo(file);
//        for(MessageInfo messageInfo: messageList){
//            Response<Class> classResponse = classService.getByName(messageInfo.getClas());
//            Class classInfo=new Class();
//            if(!classResponse.isSuccess()){
//                classInfo.setClassId("class"+String.valueOf(count));
//                count++;
//                classService.insert(classInfo);
//            }
//        }
//        //课程表
//        for(MessageInfo messageInfo: messageList){
//            Response<Course> courseResponse = courseService.getByName(messageInfo.getCourse_name());
//            Course courseInfo=new Course();
//            if(!courseResponse.isSuccess()){
//                courseInfo.setSubId(messageInfo.getCourse_id());
//                courseInfo.setSubName(messageInfo.getCourse_name());
//                courseInfo.setSubTime(messageInfo.getSub_time());
//                courseInfo.setSubAttribute(messageInfo.getCour_att());
//                courseInfo.setSubCode(messageInfo.getSub_sore());
//                courseService.insert(courseInfo);
//            }
//        }
//
//        //学生表
//        for (MessageInfo messageInfo:messageList){
//            Response<Student> studentResponse = studentService.getById(messageInfo.getStud_id());
//            Student studentInfo=new Student();
//            if(!studentResponse.isSuccess()){
//                studentInfo.setStudentId(messageInfo.getStud_id());
//                studentInfo.setName(messageInfo.getStud_name());
//                studentInfo.setPwd("123456");
//                studentInfo.setClassId(classService.getByName(messageInfo.getClas()).getData().getClassId());
//                studentInfo.setMajorId(majorService.getByName(getMajorId01(messageInfo.getClas())).getData().getMajorId());
//                studentService.insert(studentInfo);
//            }
//        }
        //学生选课表
        for(MessageInfo messageInfo:messageList){
            Response<Election> electionListResponse=electionService.getById(courseService.getByName(messageInfo.getCourse_name()).getData().getSubId(),messageInfo.getStud_id());
            Election election=new Election();
            if(!electionListResponse.isSuccess()){
                election.setStudentId(messageInfo.getStud_id());
                election.setSubId(courseService.getByName(messageInfo.getCourse_name()).getData().getSubId());
                election.setScore(messageInfo.getScore());


                //平时成绩
                election.setExper_score(election.getExper_score());
                election.setDay_score(election.getDay_score());
                election.setPro_score(election.getPro_score());
                election.setPro_ex(election.getPro_ex());
                election.setPro_day(election.getPro_day());
                electionService.insert(election);
            }

        }


        if (list.size()>0){
            res.put("log", list);
        }
        res.put("success", "导入数据成功条数:"+rowSuccess);
        res.put("error", "导入数据失败条数:"+errorCount);
        long endTime = System.currentTimeMillis();
        String time = String.valueOf((endTime - startTime) / 1000);
        res.put("time", "导入数据用时:"+time+"秒");
        return "hello world";
    }

    @PostMapping("/fileUpdate2")
    public String fileUpdate2(@RequestParam("file") MultipartFile file){
        System.out.println("文件为："+file);
        List<Compete> competeList= ComExcelUtils.getExcelInfo(file);
        System.out.println("接受文件"+competeList);
        int num=1;
        for (Compete compete: competeList){
            Response<Competition> competitionResponse=competeService.getByName(compete.getComName());
            Competition competition=new Competition();
            if(!competitionResponse.isSuccess()){
                competition.setCompetitionName("competition"+num);
                num++;
                competition.setCompetitionName(compete.getComName());
                competition.setCompetitionType(competition.getCompetitionType());
                competeService.insert(competition);
            }
        }

        return "finish";
    }
}
